package org.zjt.mock.demo.mock.servlet;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author juntao.zhang
 * @version V1.0
 * @Description: TODO
 * @Package org.zjt.mock.demo.mock.servlet
 * @date 2018/6/24 17:02
 */
@WebServlet(name = "asyncNioServlet",urlPatterns = "/asyncNioServlet",asyncSupported = true)
@Slf4j
public class AsyncNioServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }


    /**
     *  异步 servelt 
     * @since servlet 3.0
     * @param args
     */
    public  void asyncServalt(HttpServletRequest req, HttpServletResponse resp) {
        AsyncContext asyncContext = req.startAsync();
        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(AsyncEvent event) throws IOException {
                ServletResponse suppliedResponse = event.getSuppliedResponse();
                ServletOutputStream outputStream = suppliedResponse.getOutputStream();
                outputStream.print("ssssssssssssssssssss");
            }

            @Override
            public void onTimeout(AsyncEvent event) throws IOException {

            }

            @Override
            public void onError(AsyncEvent event) throws IOException {

            }

            @Override
            public void onStartAsync(AsyncEvent event) throws IOException {

            }
        });
        asyncContext.start(()-> asyncContext.complete());
    }


    /**
     * 非阻塞IO read  用于大文件读取
     *
     * @since servelt 3.1
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public  void NIORead(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        AsyncContext asyncContext = req.startAsync();

        // 非阻塞IO 读
        ServletInputStream inputStream = req.getInputStream();
        inputStream.setReadListener(new ReadListener() {
            @Override
            public void onDataAvailable() throws IOException {
                List<String> lines = IOUtils.readLines(inputStream, "utf-8");
                lines.forEach(log::info);
                asyncContext.complete();
            }

            @Override
            public void onAllDataRead() throws IOException {
                asyncContext.getResponse().getWriter().println("Finished");
                asyncContext.complete();
            }

            @Override
            public void onError(Throwable throwable) {

            }
        });
    }


    /**
     *  NIO的写出   大文件的写出
     * @param req
     * @param resp
     * @throws IOException
     * @since servlet 3.1
     */
    public  void NIOWrite(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        AsyncContext asyncContext = req.startAsync();

        // 非阻塞IO 写
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.setWriteListener(new WriteListener(){
            @Override
            public void onWritePossible() throws IOException {
                outputStream.print("aaaaaaaaaaaaaaaaaaaaaa");
                asyncContext.complete();
            }

            @Override
            public void onError(Throwable throwable) {

            }
        });
    }


}
